%function [pos rss0 n] = constrained_optimal(pix,rss, fillvalue, pixnumber, prev, equation_set,equationsymbol_set,width,height,unbelieve_number,residue)
function [pos rss0 n] = optimal_constrain(pix,rss, fillvalue, pixnumber, prev, equation_set,equationsymbol_set,width,height,residue,diff)
%[Xind,Yind,ie]=voronoi_drawxy(width,height,equation_set,equationsymbol_set);
% if ie
%     A=[];
%     b=[];
% else
    A=zeros(size(equation_set,1),2);
    b=zeros(size(equation_set,1),1);
    for i=1:size(equation_set,1)
         if equationsymbol_set(i,1)==1
            A(i,:)=-equation_set(i,1:2);
            b(i,1)=equation_set(i,3)-5;
         end
         if equationsymbol_set(i,1)==-1
            A(i,:)=equation_set(i,1:2);
            b(i,1)=-equation_set(i,3)+5;
         end
    end
    m = size(A, 1);
    % A = [zeros(m, 2) A];
    % A = [A, -ones(m,1)];
    for i = 1 : m
        no = norm(A(i, :));
        if (no > 1)
            A(i, :) = A(i, :) / no;
            b(i) = b(i) / no;
        end
    end
%end
%prev = [prev; 100];

% size(rss)
%A
%b
% size(diff)
options = optimoptions('fmincon', 'MaxIter', 100);
[~, poss] =  evalc('fmincon(@(t)(costfunction_constrain(t, pix, rss, fillvalue, pixnumber,residue, A, b, diff)), prev,[],[],[],[],[40,2,0,0],[60,4,width,height],[],options)');
%[~, poss] =  evalc('fmincon(@(t)(costfunction_constrain(t, pix, rss, fillvalue, pixnumber,residue)), prev,[],[],[],[],[40,2,0,0],[60,4,width,height],[],[])');
pos = poss(3:4);
rss0 = poss(1);
n = poss(2);
save db_copt.mat
end
